


import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;

public class VariableManager implements Drawable, MethodRelated {

	private SpinnerNumberModel smoothCostModel;
	private SpinnerNumberModel expandIterationsModel;
	private SpinnerNumberModel graphIterationsModel;
	private JToggleButton.ToggleButtonModel logFlagModel;

	public VariableManager() {
		smoothCostModel = new SpinnerNumberModel(0.0, 0.0, 1000.0, 0.1);
		expandIterationsModel = new SpinnerNumberModel(5, 0, 1000, 1);
		graphIterationsModel = new SpinnerNumberModel(2, 0, 1000, 1);
		logFlagModel = new JToggleButton.ToggleButtonModel();
	}

	public Component drawContentPane() {
		JPanel variablePane = new JPanel();
		variablePane
				.setLayout(new BoxLayout(variablePane, BoxLayout.PAGE_AXIS));

		JPanel smoothSpinnersPanel = new JPanel();
		smoothSpinnersPanel.setLayout(new BorderLayout());
		JSpinner smoothSelector = new JSpinner(smoothCostModel);
		smoothSpinnersPanel.add(new JLabel("Smooth Factor : "),
				BorderLayout.WEST);
		smoothSpinnersPanel.add(smoothSelector, BorderLayout.EAST);

		JPanel expandIterationsSpinnersPanel = new JPanel();
		expandIterationsSpinnersPanel.setLayout(new BorderLayout());
		JSpinner iterationsSelector = new JSpinner(expandIterationsModel);
		expandIterationsSpinnersPanel.add(
				new JLabel("Expansion iterations : "), BorderLayout.WEST);
		expandIterationsSpinnersPanel
				.add(iterationsSelector, BorderLayout.EAST);

		JPanel graphIterationsSpinnersPanel = new JPanel();
		graphIterationsSpinnersPanel.setLayout(new BorderLayout());
		JSpinner graphIterationsSelector = new JSpinner(graphIterationsModel);
		graphIterationsSpinnersPanel.add(new JLabel("Graph iterations : "),
				BorderLayout.WEST);
		graphIterationsSpinnersPanel.add(graphIterationsSelector,
				BorderLayout.EAST);

		JPanel logFlagPanel = new JPanel();
		logFlagPanel.setLayout(new BorderLayout());
		JCheckBox logFlagButton = new JCheckBox("Log file");
		logFlagButton.setModel(logFlagModel);
		logFlagPanel.add(logFlagButton);

		variablePane.add(smoothSpinnersPanel);
		variablePane.add(expandIterationsSpinnersPanel);
		variablePane.add(graphIterationsSpinnersPanel);
		variablePane.add(logFlagPanel);

		return variablePane;
	}

	public void setEnable(boolean enable) {
	}

	public void updateProxy() {
		JSMethodProxy jsProxy = JSMethodProxy.getInstance();

		jsProxy.setSmoothCost((Double) smoothCostModel.getValue());
		jsProxy.setExpandIterations((Integer) expandIterationsModel.getValue());
		jsProxy.setGraphIterations((Integer) graphIterationsModel.getValue());
		if (logFlagModel.isPressed() || logFlagModel.isSelected())
			jsProxy.setLogFlag(1);
		else
			jsProxy.setLogFlag(0);
	}

}
